package com.xiongxiong.yiqing.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiongxiong.yiqing.exception.Asserts;
import com.xiongxiong.yiqing.mapper.RecordCardMapper;
import com.xiongxiong.yiqing.pojo.RecordCard;
import com.xiongxiong.yiqing.pojo.SysUser;
import com.xiongxiong.yiqing.pojo.vo.FindSysUserCondition;
import com.xiongxiong.yiqing.pojo.vo.RecordCardAddOrUpdateVO;
import com.xiongxiong.yiqing.pojo.vo.RecordCardVO;
import com.xiongxiong.yiqing.service.RecordCardService;
import com.xiongxiong.yiqing.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 熊熊很酷
 * @since 2022-12-14
 */
@Service
public class RecordCardServiceImpl extends ServiceImpl<RecordCardMapper, RecordCard> implements RecordCardService {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 分页或帅选条件查询 userId=0表示查全部
     * @author 熊林飞
     * @date: 2023/2/11 23:22
     */
    @Override
    public Page<RecordCardVO> recordList(Integer pageNub, Integer pageSize, Integer id, FindSysUserCondition findCondition) {
        Page<RecordCard> page = new Page<>(pageNub, pageSize);
        QueryWrapper<RecordCard> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        System.out.println(findCondition);

        // 判断查自己还是查全部
        if(id != 0){
            wrapper.eq("user_id", id);
        }

        // 进行条件筛选
//        if (findCondition != null){
//            if(StrUtil.isNotBlank(findCondition.getUserName())){
//                wrapper.like("user_name", findCondition.getUserName());
//            }
//            if(StrUtil.isNotBlank(findCondition.getPhoneNub())){
//                wrapper.like("phone_nub", findCondition.getPhoneNub());
//            }
//            if(StrUtil.isNotBlank(findCondition.getAddress())){
//                wrapper.like("address", findCondition.getAddress());
//            }
//        }

        Page<RecordCard> cardPage = this.page(page, wrapper);
        // 查询每条记录的用户信息用户信息
        List<RecordCardVO> recordCardVOList = new ArrayList<>();
        cardPage.getRecords().forEach(record -> {
            RecordCardVO cardVO = RecordCardVO.getRecordCardVO(record);
            SysUser sysUser = sysUserService.selectUserById(Integer.valueOf(record.getUserId().toString()));
            if(sysUser != null){
                cardVO.setPhoneNub(sysUser.getPhoneNub());
                cardVO.setUserName(sysUser.getUserName());
                cardVO.setSex(sysUser.getSex());
            }
            recordCardVOList.add(cardVO);
        });
        Page<RecordCardVO> cardPageVO = new Page<>(pageNub, pageSize);
        cardPageVO.setRecords(recordCardVOList);
        cardPageVO.setTotal(cardPage.getTotal());

        return cardPageVO;
    }

    /**
     * 打卡或修改 type=1打卡 2修改
     * @return java.lang.Boolean
     * @author 熊林飞
     * @date: 2023/2/11 23:23
     */
    @Override
    public Boolean updateOrAddRecord(Integer type, RecordCardAddOrUpdateVO card) {
        RecordCard recordCard = new RecordCard();
        recordCard.setUserId(card.getUserId());
        recordCard.setBodyState(card.getBodyState());
        recordCard.setTemperature(card.getTemperature());
        recordCard.setYinOrYang(card.getYinOrYang());
        recordCard.setRecordAddress(card.getRecordAddress());
        // 新增
        if (type == 1){
            if (isRecord(card)){
                return this.save(recordCard);
            }
            Asserts.fail("今天已经打卡了");
        }
        if (type == 2 && card.getId() != null){
            recordCard.setId(card.getId());
            return this.updateById(recordCard);
        }
        return false;
    }

   /** 
    * true 表示未打卡 
    * @param card
    * @return java.lang.Boolean
    * @author 熊林飞
    * @date: 2023/2/23 14:39
    */
    public Boolean isRecord(RecordCardAddOrUpdateVO card) {
        List<RecordCard> list = this.list(new QueryWrapper<RecordCard>()
                .eq("user_id", card.getUserId())
                .like("create_time", LocalDate.now().toString()));
        System.out.println(list);
        if (list.size() == 0){
            return true;
        }
        return  false;
    }


    /**
     * 删除通过id
     * @param id id
     * @return java.lang.Boolean
     * @author 熊林飞
     * @date: 2023/2/11 23:23
     */
    @Override
    public Boolean deleteRecordById(Integer id) {
        return  this.removeById(id);
    }

}
